package cn.stylefeng.guns.animals.modular.productjpg.controller;

import cn.stylefeng.guns.animals.modular.productjpg.service.IProductJpgService;
import cn.stylefeng.guns.animals.modular.system.dao.ProductJpgMapper;
import cn.stylefeng.guns.animals.modular.system.model.Product;
import cn.stylefeng.guns.animals.modular.system.model.ProductJpg;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.roses.core.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 产品图样控制器
 *
 * @author fengshuonan
 * @Date 2020-09-15 15:58:12
 */
@Controller
@RequestMapping("/productJpg")
public class ProductJpgController extends BaseController {

    private String PREFIX = "/system/productJpg/";

    @Autowired
    ProductJpgMapper productJpgMapper;

    @Autowired
    private IProductJpgService productJpgService;

    /**
     * 跳转到产品图样首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "productJpg.html";
    }

    /**
     * 跳转到添加产品图样
     */
    @RequestMapping("/productJpg_add")
    public String productJpgAdd() {
        return PREFIX + "productJpg_add.html";
    }

    /**
     * 跳转到修改产品图样
     */
    @RequestMapping("/productJpg_update/{productJpgId}")
    public String productJpgUpdate(@PathVariable Integer productJpgId, Model model) {
        ProductJpg productJpg = productJpgService.selectById(productJpgId);
        model.addAttribute("item",productJpg);
        LogObjectHolder.me().set(productJpg);
        return PREFIX + "productJpg_edit.html";
    }

    /**
     * 跳转到查看产品图样详情
     */
    @RequestMapping("/productJpg_detail/{id}")
    public String productJpgDetail(@PathVariable Integer id, Model model) {

        ProductJpg productJpg = productJpgMapper.getJpg(id);
        model.addAttribute("item",productJpg);
        LogObjectHolder.me().set(productJpg);
        return PREFIX + "productJpg_detail.html";
    }

    /**
     * 获取产品图样列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(Product product) {
//        if (ToolUtil.isNotEmpty(product.getId())){
//            List<ProductJpg> jpgList = productJpgMapper.getJpgList(product);
//            return jpgList;
//        }
        List<ProductJpg> jpgList = productJpgMapper.getJpgList(product);
        return jpgList;
    }

    /**
     * 新增产品图样
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(ProductJpg productJpg) {
        productJpg.setCreatetime(new Date());
        productJpgService.insert(productJpg);

        return SUCCESS_TIP;
    }

    /**
     * 删除产品图样
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer productJpgId) {
        productJpgService.deleteById(productJpgId);
        return SUCCESS_TIP;
    }

    /**
     * 修改产品图样
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(ProductJpg productJpg) {
        productJpg.setUpdatetime(new Date());
        productJpgService.updateById(productJpg);

        return SUCCESS_TIP;
    }

    /**
     * 产品图样详情
     */
    @RequestMapping(value = "/detail/{productJpgId}")
    @ResponseBody
    public Object detail(@PathVariable("productJpgId") Integer productJpgId) {
        return productJpgService.selectById(productJpgId);
    }
}
